home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / lzhsourc.lzh / LZH.SRC / LZS.C < prev    next >
C/C++ Source or Header  |  1992-07-02  |  3KB  |  130 lines

  1. /*************************************************
  2.         LHarc version 1.13b (c)Yoshi, 1988-89.
  3.  
  4.         adaption to ATARI ST with TURBO-C 1.1
  5.         from LZS.ASM
  6.         by J. Moeller 1990/01/28
  7.  
  8. HTAB = 4
  9. *************************************************/
  10.  
  11. #ifndef __TOS__
  12. #error Please try assembling 'lzs.asm'!
  13. #endif
  14. #ifdef _hufst_
  15.   #define from extern
  16. #else
  17.   #define from
  18. #endif
  19.  
  20. #include <stdio.h>
  21.  #include <tos.h>
  22.  
  23. #ifdef __TOS__
  24.  #include "goodputc.h"
  25. #endif
  26. #define BUFFERSIZ    16384L          /* also defined in 'LHARC.C' */
  27. #define MAXBLK        64
  28.  
  29. unsigned char buf2 [BUFFERSIZ], /* IO-Buffer */
  30.               buf3 [BUFFERSIZ];
  31. from unsigned crctbl [256];
  32.  
  33. #define RDERR        13
  34. #define WTERR        14
  35.  
  36. #define setcrc(ch) (crc = (crc >> 8) ^ crctbl [(crc ^ (ch)) & 0xff])
  37.  
  38. extern unsigned char crcflg;
  39. extern unsigned crc;
  40. extern char *infname, *outfname;
  41. extern unsigned blkcnt;
  42. extern unsigned char flg_n;
  43.  
  44. extern void error (int errcode, char *p);
  45.  
  46. void mkcrc (void)
  47. {
  48.   register unsigned i;
  49.  
  50.   for (i = 0; i<= 255; i++)
  51.    {
  52.      register unsigned j, x;
  53.  
  54.      x = i;
  55.      for (j = 0; j <= 7; j++)
  56.       {
  57.     if (x & 1)
  58.        x = (x >> 1) ^ 0xa001;
  59.     else
  60.        x >>= 1;
  61.       }
  62.      crctbl [i] = x;
  63.    }
  64. }
  65.  
  66. /*****************************
  67.     Copy <size> Bytes
  68.     from <Source> to <Dest>
  69. *****************************/
  70.  
  71. void copyfile (FILE *Source,
  72.            FILE *Dest,
  73.            long size)
  74. {
  75.     register int ch;
  76.     long mem,r;
  77.     char *buf;
  78.  
  79.     if (size == 0L)
  80.         return;
  81.  
  82.     if (crcflg)
  83.     {
  84.         int printcount;
  85.  
  86.         printcount = 4096;
  87.         if (printcount > size)
  88.             printcount = (unsigned) size;
  89.         if (blkcnt > MAXBLK)
  90.             blkcnt = MAXBLK;
  91.         crc = 0;
  92.         while (size-- > 0 && (ch = getc (Source)) != EOF) {
  93.             setcrc (ch);
  94.             if (Dest != NULL)
  95.                if (putc (ch, Dest)==EOF) error (WTERR, outfname);
  96.             if (!flg_n)
  97.                 if (--printcount == 0) {
  98.                     if (blkcnt > 0) {
  99.                         putc ('*', stdout);
  100.                         blkcnt--;
  101.                     }
  102.                     printcount = 4096;
  103.                     if (printcount > size)
  104.                         printcount = (unsigned) size;
  105.                 }
  106.         }
  107.     } else {
  108.         mem=(long)Malloc(-1)-20000;
  109.         if (mem>0) {
  110.               if (mem>size) mem=size;
  111.           buf=(char *) Malloc(mem);
  112.           r=1;
  113.           while (size>0 && r>0) {
  114.              r=fread(buf,1,mem,Source);
  115.              size-=r;
  116.              fwrite(buf,1,mem,Dest);
  117.           }
  118.              Mfree(buf);
  119.  
  120.         } else
  121.         while (size-- > 0 && (ch = getc (Source)) != EOF)
  122.             if (Dest != NULL)
  123.                 if (putc (ch, Dest) == EOF) error (WTERR, outfname);
  124.     }
  125.     if (ferror (Source))
  126.         error (RDERR, infname);
  127.     if (Dest != NULL && ferror (Dest))
  128.         error (WTERR, outfname);
  129. }
  130.